﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Security.Principal;
using System.Threading;

namespace Milan.Net
{
	/// <summary>
	/// A generic RPC client
	/// </summary>
	public class RpcClient
	{
		public RpcClient(string pipeName, int timeout)
		{
			m_pipeClient = new NamedPipeClientStream(".",pipeName, PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation);
			m_timeout = timeout;
		}
		private NamedPipeClientStream m_pipeClient;
		private int m_timeout;
		private RpcServer.StreamString m_ss;
		public void Connect()
		{
			m_pipeClient.Connect();
			m_ss = new RpcServer.StreamString(m_pipeClient);
			if (m_ss.ReadString() != "{ok}")
			{

			}
		}
		/// <summary>
		/// Transact a command to the server
		/// </summary>
		/// <param name="command"></param>
		/// <returns></returns>
		public string Transact(string command)
		{
			m_ss.WriteString(command);
			return m_ss.ReadString();
		}
		public void Disconnect()
		{
			m_pipeClient.Close();
		}

	}
}
